use std::cmp::Ordering;
use rand::Rng;
use std::io;

fn main() {
    println!("=========== 猜数字游戏 ============");

    let x = rand::thread_rng().gen_range(0..101);
    println!("生成的随机数为: {}", x);

    loop {
        println!("请输入一个1到100之间的数:");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("读取失败");
        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("输入无效，请输入一个有效的数字");
                continue;
            }
        };

        match guess.cmp(&x) {
            Ordering::Less => println!("猜小了"),
            Ordering::Greater => println!("猜大了"),
            Ordering::Equal => {
                println!("恭喜你，猜对了");
                break;
            }
        }
    }
}
